// import request from '@/utils/request'
import { login, reqUserInfo, reqStaffBaseInfo } from '@/api/user'
// import { Message } from 'element-ui'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    setUserInfo(state, newUserInfo) {
      state.userInfo = newUserInfo
    },
    removeToken(state) {
      state.token = ''
      removeToken()
    },
    removeUserInfo(state) {
      state.userInfo = {}
    }

  },
  actions: {
    // async actionsGetToken({ commit }, data) {
    //   const res = await login(data)
    //   commit('setToken', res)
    // },
    actionsGetToken({ commit }, data) {
      return new Promise((resolve, reject) => {
        login(data).then(res => {
          commit('setToken', res)
          resolve(res)
        }
        ).catch(error => {
          reject(error)
        })
      })
    },
    async actionsGetUserInfo({ commit }) {
      const res = await reqUserInfo()
      const resPhoto = await reqStaffBaseInfo(res.userId)

      commit('setUserInfo', {
        ...res,
        ...resPhoto
      })
      return {
        ...res,
        ...resPhoto
      }
    },
    removeTokenAndUserInfo({ commit }) {
      commit('removeToken')
      commit('removeUserInfo')
      resetRouter()
      commit('permission/setRoute', [], { root: true })
      // 5. 清除主题色信息
      commit('settings/resetTheme', null, { root: true })
    }
  }
}
